package cn.edu.gench.learn.day8;


import java.sql.*;
import java.text.MessageFormat;


public class ConnectionDB {

    public static void main(String[] args) {
            // 驱动程序名
            String driver = "com.mysql.jdbc.Driver";
            // URL指向要访问的数据库名world
            String url = "jdbc:mysql://127.0.0.1:3306/learn?useUnicode=true&characterEncoding=utf8";
            try {
                // 加载驱动程序
                Class.forName(driver);
                // 连续数据库
                Connection conn = DriverManager.getConnection(url, "root", "");
                Statement statement = conn.createStatement();
                // 要执行的SQL语句
                String insertSql = MessageFormat.format("insert into d8_book(name,created_at) VALUES(\"{0}\",now())","命题皇后");
                System.out.println(insertSql);
                // 结果集
                statement.execute(insertSql);
                String querySql  = "select * from d8_book";
                ResultSet rs = statement.executeQuery(querySql);
                while(rs.next())  {
                    System.out.print(rs.getString("id")+" - ");
                    System.out.print(rs.getString("name")+" - ");
                    System.out.print(rs.getString("created_at")+" - ");
                    System.out.println();
                }
                rs.close();
                statement.close();
                conn.close();
            } catch(ClassNotFoundException e) {
                System.out.println("Sorry,can`t find the Driver!");
                e.printStackTrace();
            } catch(SQLException e) {
                e.printStackTrace();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
}
